package com.example.leetcode;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @description: TODO
 * @author: Sarah
 * @modified By: Sarah
 * @date: Created in 2021/8/3 13:39
 * @version:v1.0
 */
public class wer {
    public int findUnsortedSubarray(int[] nums) {
        int[] numsSorted = Arrays.copyOf(nums, nums.length);
        Arrays.sort(numsSorted);
        int x=0,y=0,fx=0,fy=0;
        for(int i=0;i< nums.length;i++){
            if(fx==0&&numsSorted[i]==nums[i]){
                x++;
            }else{
                fx=1;
            }
            if(fy==0&&numsSorted[nums.length-i-1]==nums[nums.length-i-1]){
                y++;
            }else{
                fy=1;
            }
        }
        if(x+y>nums.length){
            return 0;
        }


        return nums.length-x-y;

    }
}
